package com.zunipe.until;

import com.zunipe.ZunipeTools;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.text.LiteralText;

import java.time.LocalDate;
import java.time.LocalTime;

public class SignInFunction
{
    public static void SignIn(MinecraftClient client)
    {
        Util.autoSignIn = !Util.autoSignIn;
        if (Util.autoSignIn)
        {
            client.player.sendMessage(new LiteralText("§l自动签到 §2§l开启"), true);
            new Thread(() ->
            {
                while (Util.autoSignIn)
                {
                    LocalTime time = LocalTime.now();
                    LocalDate date = LocalDate.now();
                    if (time.getHour() == 23 && time.getMinute() == 59)
                    {
                        client.player.sendChatMessage("/signin gui");
                        try
                        {
                            Thread.sleep(500);
                            int slot = date.getDayOfMonth();
                            while (client.currentScreen != null && client.currentScreen.getTitle().getString().contains("签到"))
                            {
                                if (client.currentScreen instanceof HandledScreen<?> screen)
                                {
                                    client.interactionManager.clickSlot(screen.getScreenHandler().syncId, 9 + slot, 0, SlotActionType.PICKUP, client.player);
                                }
                                Thread.sleep(100);
                            }
                            Util.autoSignIn = false;
                            client.player.sendMessage(new LiteralText("§l自动签到 §4§l关闭"), true);
                        } catch (InterruptedException e)
                        {
                            ZunipeTools.LOGGER.info(e.getMessage());
                        }
                    }
                    try
                    {
                        Thread.sleep(500);
                    } catch (InterruptedException e)
                    {
                        ZunipeTools.LOGGER.info(e.getMessage());
                    }
                }
            }).start();
        }
        else
        {
            client.player.sendMessage(new LiteralText("§l自动签到 §4§l关闭"), true);
        }
    }
}
